import time

from selenium import webdriver
from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By

from core.UploadWindow import Upload_window


def upload_event(event_title, event_desc, address):
    # 打开浏览器
    driver = webdriver.Chrome()
    # 打开网址
    print("打开网址")
    driver.get("http://1/#/")
    #输入手机号
    print("输入手机号")
    login_inputs =  driver.find_elements(by=By.TAG_NAME,value  = 'input')
    for e in login_inputs:
        if e.get_attribute('name')=='systemPhone':
            e.send_keys('130000000000')
            break
    time.sleep(1)
    #输入密码
    print("输入密码")
    for e in login_inputs:
        if e.get_attribute('name') == 'systemAccountSecret':
            e.send_keys('12345647#')
            break
    time.sleep(1)

    # 获取登录后的标识
    home_sign_exist = True
    while home_sign_exist:
        try:
            home_sign = driver.find_element(by=By.CLASS_NAME, value='topic')  # 查找span标签，可根据实际标签名修改
            home_sign_exist = False
        except NoSuchElementException:
            print("等待进入首页")
            time.sleep(1)
    # 判断是否存在
    print("进入首页")
    menuItems = driver.find_elements(by=By.CLASS_NAME, value='menuItem')
    print("点击事件池")
    for menuItem in menuItems:
        if menuItem.text == "事件池":
            menuItem.click()
            break

    print("等待页面响应2秒")
    time.sleep(2)

    print("开始查找操作按钮所在的样式范围")
    app = driver.find_element(by=By.ID, value='app')
    appWrapper = app.find_element(by=By.CLASS_NAME, value='app-wrapper')
    mainContainer = appWrapper.find_element(by=By.CLASS_NAME, value='main-container')
    sidebarContainer = mainContainer.find_element(by=By.CLASS_NAME, value='sidebar-container')
    elScrollbar = sidebarContainer.find_element(by=By.CLASS_NAME, value='el-scrollbar')
    elScrollbarDivs = elScrollbar.find_elements(by=By.TAG_NAME, value='div')

    _elScrollbarDiv = ''

    for elScrollbarDiv in elScrollbarDivs:
        if elScrollbarDiv.get_attribute('class') == 'scrollbar-wrapper el-scrollbar__wrap':
            _elScrollbarDiv = elScrollbarDiv
            break

    spans = _elScrollbarDiv.find_elements(by=By.TAG_NAME, value='span')

    time.sleep(1)
    print("点击全部事件")
    for span in spans:
        if span.text == '全部事件':
            span.click()
            break

    time.sleep(2)
    print("点击添加事件")
    buttons = driver.find_elements(by=By.TAG_NAME, value='span')
    for e in buttons:
        if e.get_attribute('class') == 'tmpl-button__ml4':
            if e.text == "添加":
                e.click()
                break
    time.sleep(2)
    print("填写事件表单")
    evenTitleObj = ''
    inputs = driver.find_elements(by=By.TAG_NAME, value='input')
    for input in inputs:
        if input.get_attribute('placeholder') == "请输入事件名称":
            print("填写事件表单-填写事件标题")
            input.send_keys(event_title)
        if input.get_attribute('placeholder') == "请选择事件类型":
            print("填写事件表单-填写事件标题")
            input.click()
            time.sleep(2)
            selectItem = driver.find_elements(by=By.CLASS_NAME, value='el-select-dropdown__item')
            for e in selectItem:
                if e.text == '治安问题':
                    e.click()
                    break

    time.sleep(2)
    evenType = []
    inputs_now = driver.find_elements(by=By.TAG_NAME, value='input')
    for input in inputs_now:
        if input.get_attribute('placeholder') == "请选择事件类型":
            print("============装配===========")
            evenType.append(input)

    print("点击第二个类型")
    evenType[1].click()
    time.sleep(1)
    selectItem = driver.find_elements(by=By.CLASS_NAME, value='el-select-dropdown__item')
    for e in selectItem:
        if e.text == '传销组织':
            e.click()
            break

    time.sleep(2)
    textareas = driver.find_elements(by=By.TAG_NAME, value='textarea')
    for textarea in textareas:
        if textarea.get_attribute('placeholder') == "事件内容最多255个字":
            print("填写事件表单-填写事件描述")
            textarea.send_keys(event_desc)
            break
    time.sleep(2)
    print("上传图片")
    formItems = driver.find_elements(by=By.CLASS_NAME, value='el-form-item')
    for formItem in formItems:
        lables = formItem.find_elements(by=By.TAG_NAME, value='label')
        for lable in lables:
            if lable.get_attribute('for') == "imageUrl":
                spans = formItem.find_elements(by=By.TAG_NAME, value='span')
                for span in spans:
                    if span.text == "点击上传":
                        print("点击上传图片")
                        span.click()
                        time.sleep(2)
                        print("开始捕获window窗口")
                        Upload_window.upload_file('D:\临时文件', '微信图片_20241101150712.jpg')
                        break
    time.sleep(2)
    print("上传视频")
    formItems = driver.find_elements(by=By.CLASS_NAME, value='el-form-item')
    for formItem in formItems:
        lables = formItem.find_elements(by=By.TAG_NAME, value='label')
        for lable in lables:
            if lable.get_attribute('for') == "videoUrl":
                spans = formItem.find_elements(by=By.TAG_NAME, value='span')
                for span in spans:
                    if span.text == "点击上传":
                        print("点击上传视频")
                        span.click()
                        time.sleep(2)
                        print("开始捕获window窗口")
                        Upload_window.upload_file('D:\临时文件', '屏幕录制 2024-11-14 140320.mp4')
                        break
    time.sleep(2)
    print("点击绘制按钮")
    map_spans = driver.find_elements(by=By.TAG_NAME, value='span')
    for map_span in map_spans:
        if map_span.text == '绘制':
            map_span.click()
            break
    time.sleep(2)
    print("绘制框中搜索")
    draw_divs = driver.find_elements(by=By.TAG_NAME, value='div')
    for draw_div in draw_divs:
        if draw_div.get_attribute('aria-label') == "标注位置":
            draw_divs_div = draw_div.find_elements(by=By.TAG_NAME, value='div')
            for e in draw_divs_div:
                if e.get_attribute('class') == "invest_store_search":
                    draw_divs_input_opt = e.find_element(by=By.TAG_NAME, value='input')
                    draw_divs_input_opt.send_keys(address)
                    time.sleep(2)
                    draw_divs_input_ok = e.find_element(by=By.CLASS_NAME, value='invest_store_button')
                    draw_divs_input_ok.click()
                    break

    print("确认绘制位置")
    time.sleep(2)
    button_oks = driver.find_elements(by=By.TAG_NAME, value='span')
    for button_ok in button_oks:
        if button_ok.text == '确定':
            button_ok.click()
            break
    time.sleep(2)
    button_saves = driver.find_elements(by=By.TAG_NAME, value='span')
    for button_save in button_saves:
        if button_save.text == '保存':
            button_save.click()
            break
    print("操作完成")
    print("继续保持画面")
    time.sleep(999)


if __name__ == '__main__':
    upload_event("自动化_上传测试_S_1_title", "自动化_上传测试_S_1_desc", "星月城")
